<div class="rightcontent rc-bg-grey">
    <div class="rc-pagepadding">

        <div class="rc-pannel rc-searchbox">
            <form class="layui-form " method="get">
                <div class="rc-flex-vertical-center rc-searchbox">
                    <div class="layui-inline ">
                        <label class="layui-form-label">任务名称</label>
                        <div class="layui-input-inline ">
                            <input type="text" name="title" placeholder="" value="<?php echo request()->get('title', ''); ?>" autocomplete="off" class="layui-input">
                        </div>
                    </div>



                    <div class="layui-inline">
                        <div class="layui-form-label">
                            <button type="submit" class="layui-btn layui-btn-sm layui-btn-primary"><i class="iconfont">&#xeafe;</i> 搜索</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>


        <div class=" rc-pannel rc-margin-top-default">
            <div class=" rc-pannel-header">
                <div class="rc-flex layui-form toolbar rc-flex-justify-between">
                    <div class=" rc-flex rc-flex-vertical-center ">

                    </div>
                    <div>
                        <a href="/admin/cnf/CrontabTask/add" class="layui-btn layui-btn-primary layui-btn-sm"><i class="iconfont">&#xe60e;</i> 添加计划任务</a>

                    </div>
                </div>


            </div>


            <div class="rc-pannel-body layui-form rc-pagepadding">

                <table class="layui-table">
                    <colgroup>
                        <col>
                        <col>
                        <col>
                        <col>
                        <col style="width: 140px;">

                    </colgroup>
                    <thead>
                        <tr>
                            <th>任务名称</th>
                            <th>调用事件</th>
                            <th>执行周期</th>
                            <th>状态</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ($tasks as $task) {
                        ?>
                            <tr>
                                <td><?php echo $task->title ?></td>
                                <td><?php echo $task->tasktype == 1 ? $task->eventdesc : '自定义事件：' . $task->event ?></td>
                                <td>
                                    <?php
                                    switch ($task->cycle) {
                                        case 1:
                                            echo '每' . $task->minute . '分钟';
                                            break;
                                        case 2:
                                            echo '每' . $task->hour . '小时';
                                            break;

                                        case 3:
                                            echo '每' . $task->day . '天' . date('H时i分', strtotime($task->time));
                                            break;
                                        case 4:
                                            $weekinfo = ['日',  '一', '二', '三', '四', '五', '六'];
                                            echo '每周' . $weekinfo[$task->week] . ' ' . date('H时i分', strtotime($task->time));
                                            break;
                                        case 5:
                                            echo '每月' . $task->monthday . '号' . date('H时i分', strtotime($task->time));
                                            break;
                                    }
                                    ?>
                                </td>
                                <td>
                                    <input value="<?php echo $task->id; ?>" lay-filter="setStatus" type="checkbox" name="status" <?php echo $task->status == 1 ? 'checked' : '' ?> title="开启|关闭" lay-skin="switch">
                                </td>
                                <td>

                                    <div class="rc-opbar" style="width: 150px;margin-left:15px">
                                        <a class="info " href="/admin/cnf/CrontabTask/edit?id=<?php echo $task->id ?>"> <i class="iconfont">&#xe643;</i> 编辑</a>
                                        <a taskid="<?php echo $task->id ?>" class="error delete" href="javascript:;"> <i class="iconfont">&#xe607;</i> 删除</a>
                                    </div>

                                </td>

                            </tr>
                        <?php
                        }
                        ?>
                    </tbody>
                </table>
            </div>

            <div class="rc-pannel-footer">
                <div class="pagebar" id="pagebar" style="text-align: right;">
                    <?php echo paginator($tasks->total(), 20) ?>
                </div>

            </div>
        </div>
    </div>
</div>
<script>
    layui.use(function() {

        var layer = layui.layer;

        var form = layui.form;
        form.on('switch(setStatus)', function(data) {
            var elem = data.elem; // 获得 checkbox 原始 DOM 对象
            var checked = elem.checked; // 获得 checkbox 选中状态
            var value = elem.value; // 获得 checkbox 值
            var othis = data.othis; // 获得 checkbox 元素被替换后的 jQuery 对象
            var status = checked ? 1 : -1;


            $.ajax({
                url: '/admin/cnf/CrontabTask/setStatus', //请求路径
                type: "POST", //请求方式
                data: {
                    id: value,
                    status: status
                },
                success: function(data) {
                    if (data.code == 0) {
                        window.location.reload();
                    } else {
                        toastr_error(data.message);
                    }
                    layer.closeAll();
                },
                error: function() {
                    layer.closeAll();
                },

            });

        });



        $('.delete').click(function() {
            var taskid = $(this).attr('taskid');
            layer.confirm('确定要删除么？', {
                icon: 3
            }, function() {
                $.ajax({
                    url: '/admin/cnf/CrontabTask/delete?id=' + taskid, //请求路径
                    type: "POST", //请求方式

                    success: function(data) {
                        if (data.code == 0) {
                            window.location.reload();
                        } else {
                            toastr_error(data.message);
                        }
                        layer.closeAll();
                    },
                    error: function() {
                        layer.closeAll();
                    },

                });


            }, function() {



            });

        });

    });
</script>